/**
 * Created by WT-SUN on 2017/4/6.
 */
$(function () {
    $('#orgTb').datagrid({
        view: detailview,
        rownumbers: true,//显示行号
        striped: true,
        fit: true,
        singleSelect: true,
        sortOrder: true,
        fitColumns: true,
        pagination: true,
        toolbar: '#toolbar',
        url: ROOT_PATH + '/occupationalHealth/hseServiceOrg/serviceOrgTable.shtml',
        queryParams:{
            sort:'addTime',
            order:'desc'
        },
        detailFormatter:function(index,row){
            return '<div class="orgddv" style="padding:5px 0"></div>';
        },
        onExpandRow: function(index,row){
            if (typeof ($('#lastIndex').val())!='undefined'&&$('#lastIndex').val()*1!=index){
                $(this).datagrid('collapseRow',$('#lastIndex').val());
                $('.orgddv').empty();
            }
            var ddv = $(this).datagrid('getRowDetail',index).find('div.orgddv');
            ddv.panel({
                border:false,
                cache:false,
                href:ROOT_PATH + '/occupationalHealth/hseServiceOrg/info.shtml?id=' + row.id+'&type=info',
                onLoad:function(){
                    $('#lastIndex').val(index);
                    $('#orgTb').datagrid('fixDetailRowHeight',index);
                }
            });
            $('#orgTb').datagrid('fixDetailRowHeight',index);
        },
        columns: [[
            {
                field: 'id',
                checkbox: true
            },
            {field: 'serviceName', title: '服务机构名称', width: 100},
            {field: 'serviceTypeName', title: '服务机构类型', width: 100},
            {field: 'aptitudeGrade', title: '资质等级', width: 100},
            {field: 'aptitudeNumber', title: '资质号', width: 100},
            {field: 'startTime', title: '开始日期', width: 100},
            {field: 'endTime', title: '结束日期', width: 100}
        ]]
    });
    queryComboData();
    $('#btn_org_add').on('click',function () {
        addOrg();
    });
    $('#btn_org_delete').on('click',function () {
        deleteOrg();
    });
    $('#btn_org_edit').on('click',function () {
        editOrg();
    });
    $('#btn_clear').on("click", function () {
        $('#queryForm').form('reset');
    });
});

function queryComboData() {
    var code=CONSTANT.DictionaryTypeDefConstants.OHE_SERVICE_TYPE;
    var EMPTY_OPTION = {
        id: '',
        text: '请选择'
    };
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            $('#serviceTypeQuery').combobox({
                loadFilter: function (data) {
                    data.unshift(EMPTY_OPTION);
                    return data;
                },
                data: result,
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
                onSelect: function (record) {
                }
            });
        }
    });
}
/**
 * 重新加载表格
 */
function reLoadData() {
    $('#orgTb').datagrid('clearSelections');
    var queryParams = $('#queryForm').serializeJson();
    queryParams["sort"] = 'addTime';
    queryParams["order"] = 'desc';
    $('#orgTb').datagrid('options').queryParams = queryParams;
    $("#orgTb").datagrid('reload', ROOT_PATH + '/occupationalHealth/hseServiceOrg/serviceOrgTable.shtml');
}
/**
 * 打开弹框
 * @param title
 * @param content
 */
function openLayers(title, content) {
    layer.open({
        type: 2,
        resize: false,
        title: title,
        area: ['700px', '80%'],
        content: content
    });
}
/**
 * 打开新建
 */
function addOrg() {
    var title = '新增服务机构';
    var content = ROOT_PATH + '/occupationalHealth/hseServiceOrg/add.shtml';
    openLayers(title, content);
}
/**
 * 删除
 */
function deleteOrg() {
    var rows = $('#orgTb').datagrid('getSelections');

    if (rows.length == 0) {
        layer.alert("选择需要删除的行！");
        return
    }
    var id = rows[0].id;
    layer.confirm('是否确定删除当前选中数据？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.ajax({
            type: 'post',
            url: ROOT_PATH + '/occupationalHealth/hseServiceOrg/deleteById.shtml',
            data: {id: id},
            dataType: "json",
            success: function (result) {
                //result = $.parseJSON(result);
                if (result.success) {
                    $('#orgTb').datagrid('clearSelections');
                    reLoadData();
                    layer.msg('删除成功', {
                        icon: 1,
                        time: 1000
                    });
                }
                else {
                    layer.msg('删除失败', {
                        icon: 2,
                        time: 1000
                    });
                }
            }
        });
    });
}
/**
 * 编辑
 */
function editOrg() {
    var rows = $('#orgTb').datagrid('getSelections');
    if (rows.length == 0) {
        layer.alert("选择需要编辑的行！");
        return
    }
    var id = rows[0].id;
    var title = '编辑服务机构';
    var content = ROOT_PATH + '/occupationalHealth/hseServiceOrg/info.shtml?id=' + id+'&type=edit';
    openLayers(title, content);
}